<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Category as CategoryModel;
use think\facade\View;
use think\response\Json;

class Article extends AdminBase
{
    public function index(): string
    {
        $category   = new \app\common\business\Category($this->websiteCode);
        $categories = $category->getCategoryTree($this->cdnDomain);
        View::assign(['categories' => json_encode($categories, JSON_UNESCAPED_UNICODE)]);

        return View::fetch();
    }

    public function lists()
    {
        // 参数校验
        $categoryId = $this->request->param("category_id", 0, 'intval');
        if (!$categoryId) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $list = $this->request->param('list', 0, 'intval');
        View::assign(['categoryId' => $categoryId, 'list' => $list]);

        $flagBusiness = new \app\common\business\Flag($this->websiteCode);
        $flags        = $flagBusiness->getAllFlag();
        View::assign(['flags' => $flags]);

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);

        $categoryId = $categoryBusiness->getRedirectCategoryId($categoryId);
        $category   = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        if ($category['type'] == CategoryModel::SINGLE_PAGE) {
            $articleInfo = $this->_list($categoryId);
            $param       = ['category_id' => $categoryId, 'website_code' => $this->websiteCode];
            if (!empty($articleInfo) && isset($articleInfo['rows']) && count($articleInfo['rows'])) {
                $articleOne = current($articleInfo['rows']);
                return redirect((string)url('article/edit', array_merge($param, ['id' => $articleOne['id']])));
            } else {
                return redirect((string)url('article/add', $param));
            }
        }

        View::assign(['category' => $category]);

        return View::fetch();
    }

    private function _list(int $categoryId): array
    {
        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return [];
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return [];
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return [];
        }

        // 文档列表
        $where           = [['category_id', 'in', array_merge([$categoryId], $categoryBusiness->getCurCategoryChildrenIds($categoryId))]];
        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        return $articleBusiness->getArticleList($this->cdnDomain, 1, 1, $where);
    }

    public function list(): Json
    {
        // 参数校验
        $categoryId = $this->request->param("category_id", 0, 'intval');
        if (empty($categoryId)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        // 文档列表
        $page   = $this->request->param('page', 1, 'intval');
        $limit  = $this->request->param('limit', 10, 'intval');
        $title  = $this->request->param('title', '', 'trim');
        $status = $this->request->param('status', 0, 'intval');

        $where = [['category_id', 'in', array_merge([$categoryId], $categoryBusiness->getCurCategoryChildrenIds($categoryId))]];
        if ($title) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $flagCode     = $this->request->param('flag_code', '', 'trim');
        $flagBusiness = new \app\common\business\Flag($this->websiteCode);
        $flags        = $flagBusiness->getAllFlag();
        $flag         = $flags[$flagCode] ?? [];
        $flagId       = $flag['id'] ?? 0;

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $articleInfo     = $articleBusiness->getArticleList($this->cdnDomain, $page, $limit, $where, $flagId);

        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $articleInfo['rows'], $articleInfo['count']);
    }

    public function add(): string
    {
        // 参数校验
        $categoryId = $this->request->param("category_id", 0, 'intval');
        if (!$categoryId) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(['categoryId' => $categoryId]);

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(['category' => $category]);

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            View::assign(['msg' => self::_MODULE_DISABLE_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $lastArticle     = $articleBusiness->findLastArticleSortByCategoryId($categoryId);
        $lastSort        = isset($lastArticle['sort']) ? $lastArticle['sort'] + 1 : 100;

        // 字段信息
        $fields = $categoryBusiness->getArticleFields($categoryId);

        View::assign(['fields' => $fields, 'sort' => $lastSort, 'exts' => implode('|', array_merge(self::_ALLOW_PICTURE_EXT,self::_ALLOW_FILE_EXT))]);


        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        // 参数校验
        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $articleData = [
            'category_id'      => $this->request->param("category_id", 0, 'intval'),
            'title'            => $this->request->param("title", '', 'trim'),
            'sort'             => $this->request->param("sort", 0, 'intval'),
            'hit'              => $this->request->param('hit', 0, 'intval'),
            'status'           => $this->request->param("status", 0, 'intval'),
            'image'            => $this->request->param('image', '', 'trim'),
            'meta_title'       => $this->request->param('meta_title', '', 'trim'),
            'meta_keyword'     => $this->request->param('meta_keyword', '', 'trim'),
            'meta_description' => $this->request->param('meta_description', '', 'trim'),
            'created_at'       => $this->request->param('created_at', '', 'trim'),
        ];

        $hasImageAttribute = $this->request->param('has_image_attribute');
        if ($hasImageAttribute) {
            $imageAttributeId = $this->request->param('image_attribute_id');

            // image_attribute_value_id[attribute_id][] // 存放用户选择的image_attribute_value_id
            $imageAttributeValueIds = $this->request->post('image_attribute_value_id');

            // image_attribute_url[attribute_id][attribute_value_id][] // 存放用户上传的图片URL
            $imageAttributeUrls = $this->request->post('image_attribute_url');

            // image_attribute_text[attribute_id][attribute_value_id][] // 存放用户上传的图片说明
            $imageAttributeTexts = $this->request->post('image_attribute_text');

            $imageAttributes = ['attribute_id' => intval($imageAttributeId)];
            foreach ($imageAttributeValueIds as $attributeId => $imageAttributeValue) {
                if (count($imageAttributeValue) >= 1 && !$imageAttributeValue[0]) {
                    break;
                }

                $imageAttributeUrls  = $imageAttributeUrls[$attributeId];  // 图片url
                $imageAttributeTexts = $imageAttributeTexts[$attributeId]; // 图片说明

                foreach ($imageAttributeUrls as $attributeValueId => $urls) {
                    $images = [];
                    foreach ($urls as $key => $url) {
                        $images[] = [
                            'image' => $url,
                            'title' => $imageAttributeTexts[$attributeValueId][$key],
                        ];
                    }

                    $imageAttributes['attribute_values'][] = [
                        'attribute_value_id' => intval($attributeValueId),
                        'images'             => $images,
                    ];
                }
            }

            $articleData['images']              = [];
            $articleData['image_attribute']     = $imageAttributes;
            $articleData['has_image_attribute'] = true;
        } else {
            // 简单图集
            $articleImages = [];
            $images        = $this->request->param('images', []);
            $imageTitles   = $this->request->param('image_titles', []);
            if (is_array($images) && count($images) > 0) {
                foreach ($images as $k => $value) {
                    $articleImages[] = ['title' => $imageTitles[$k], 'image' => $value];
                }
            }

            $articleData['images']              = $articleImages;
            $articleData['image_attribute']     = ['attribute_values' => []];
            $articleData['has_image_attribute'] = false;
        }

        // 属性
        $attributeValueIds = $this->request->param('attribute_value_id');
        $attributes        = [];
        if ($attributeValueIds) {
            foreach ($attributeValueIds as $attributeId => $attributeValue) {
                if (count($attributeValue) >= 1 && !$attributeValue[0]) {
                    continue;
                }
                foreach ($attributeValue as $key => $attributeValueId) {
                    $attributeValue[$key] = intval($attributeValueId);
                }
                $attributes[] = ['attribute_id' => intval($attributeId), 'attribute_value_ids' => $attributeValue];
            }
        }
        $articleData['attributes'] = $attributes;

        // 其他字段
        $extend = $this->request->param('extend', []);
        if ($extend) {
            foreach ($extend as $key => $value) {
                $articleData['extend_fields'][] = ['code' => $key, 'value' => $value];
            }
        }

        // 添加文档
        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->addArticle($this->cdnDomain, $articleData);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        // 参数校验
        $categoryId = $this->request->param("category_id", 0, 'intval');
        if (!$categoryId) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            View::assign(['msg' => self::_MODULE_DISABLE_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $article         = $articleBusiness->findArticleById($this->cdnDomain, $id, $categoryBusiness->getCategories());
        if (empty($article)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $articleCategory = $article['category'];
        View::assign(['category' => $articleCategory]);

        $normalAttributes = $articleBusiness->mergeNormalAttribute($article['normal_attributes'], $articleCategory['attributes']);
        View::assign([
            'id'               => $article['id'],
            'images'           => $article['images'],
            'imageAttribute'   => $article['image_attribute'],
            'normalAttributes' => $normalAttributes,
            'categoryId'       => $article['category_id'],
            'fields'           => $article['extend_fields'],
            'exts'             => implode('|', array_merge(self::_ALLOW_PICTURE_EXT,self::_ALLOW_FILE_EXT)),
        ]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        // 参数校验
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $articleData = [
            'category_id'      => $this->request->param("category_id", 0, 'intval'),
            'title'            => $this->request->param("title", '', 'trim'),
            'sort'             => $this->request->param("sort", 0, 'intval'),
            'hit'              => $this->request->param('hit', 0, 'intval'),
            'status'           => $this->request->param("status", 0, 'intval'),
            'image'            => $this->request->param('image', '', 'trim'),
            'meta_title'       => $this->request->param('meta_title', '', 'trim'),
            'meta_keyword'     => $this->request->param('meta_keyword', '', 'trim'),
            'meta_description' => $this->request->param('meta_description', '', 'trim'),
            'created_at'       => $this->request->param('created_at', '', 'trim'),
        ];

        $hasImageAttribute = $this->request->param('has_image_attribute');
        if ($hasImageAttribute) {
            $imageAttributeId = $this->request->param('image_attribute_id');

            // image_attribute_value_id[attribute_id][] // 存放用户选择的image_attribute_value_id
            $imageAttributeValueIds = $this->request->post('image_attribute_value_id');

            // image_attribute_url[attribute_id][attribute_value_id][] // 存放用户上传的图片URL
            $imageAttributeUrls = $this->request->post('image_attribute_url');

            // image_attribute_text[attribute_id][attribute_value_id][] // 存放用户上传的图片说明
            $imageAttributeTexts = $this->request->post('image_attribute_text');

            $imageAttributes = ['attribute_id' => intval($imageAttributeId)];
            foreach ($imageAttributeValueIds as $attributeId => $imageAttributeValue) {
                if (count($imageAttributeValue) >= 1 && !$imageAttributeValue[0]) {
                    break;
                }

                $imageAttributeUrls  = $imageAttributeUrls[$attributeId];  // 图片url
                $imageAttributeTexts = $imageAttributeTexts[$attributeId]; // 图片说明

                foreach ($imageAttributeUrls as $attributeValueId => $urls) {
                    $images = [];
                    foreach ($urls as $key => $url) {
                        $images[] = [
                            'image' => $url,
                            'title' => $imageAttributeTexts[$attributeValueId][$key],
                        ];
                    }

                    $imageAttributes['attribute_values'][] = [
                        'attribute_value_id' => intval($attributeValueId),
                        'images'             => $images,
                    ];
                }
            }

            $articleData['images']              = [];
            $articleData['image_attribute']     = $imageAttributes;
            $articleData['has_image_attribute'] = true;
        } else {
            // 简单图集
            $articleImages = [];
            $images        = $this->request->param('images', []);
            $imageTitles   = $this->request->param('image_titles', []);
            if (is_array($images) && count($images) > 0) {
                foreach ($images as $k => $value) {
                    $articleImages[] = ['title' => $imageTitles[$k], 'image' => $value];
                }
            }

            $articleData['images']              = $articleImages;
            $articleData['image_attribute']     = ['attribute_values' => []];
            $articleData['has_image_attribute'] = false;
        }

        // 属性
        $attributeValueIds = $this->request->param('attribute_value_id');
        $attributes        = [];
        if ($attributeValueIds) {
            foreach ($attributeValueIds as $attributeId => $attributeValue) {
                if (count($attributeValue) >= 1 && !$attributeValue[0]) {
                    continue;
                }
                foreach ($attributeValue as $key => $attributeValueId) {
                    $attributeValue[$key] = intval($attributeValueId);
                }
                $attributes[] = ['attribute_id' => intval($attributeId), 'attribute_value_ids' => $attributeValue];
            }
        }
        $articleData['attributes'] = $attributes;

        // 其他字段
        $extend = $this->request->param('extend', []);
        if ($extend) {
            foreach ($extend as $key => $value) {
                $articleData['extend_fields'][] = ['code' => $key, 'value' => $value];
            }
        }

        // 编辑文档
        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->updateArticle($this->cdnDomain, $id, $articleData);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        // 参数校验
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        // 字段信息
        $fields = $categoryBusiness->getArticleFields($categoryId);

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->deleteArticle($id, $fields);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->batchArticleSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function batchDoDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $ids = $this->request->param("ids");
        if ($ids == "") {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        $ids = explode(",", $ids);
        foreach ($ids as $k => $v) {
            $ids[$k] = intval($v);
        }

        // 字段信息
        $fields = $categoryBusiness->getArticleFields($categoryId);

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->batchDeleteArticle($ids, $fields);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doFlag(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $ids     = $this->request->param('ids', '', 'trim');
        $operate = $this->request->param('operate', '0', 'intval');
        $flagId  = $this->request->param('flag_id', '0', 'intval');
        if (empty($ids) || $operate == 0) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->batchUpdateArticleFlagId(explode(',', $ids), $operate, $flagId);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doCheck(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $categoryId = $this->request->param('category_id', 0, 'intval');
        if (!$categoryId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 栏目信息
        $categoryBusiness = new \app\common\business\Category($this->websiteCode);
        $category         = $categoryBusiness->findCategoryById($this->cdnDomain, $categoryId);
        if (empty($category)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        // 模型信息
        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($category['module_id']);
        if (empty($module)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if ($module['status'] == self::_IS_DISABLE_STATUS) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_MODULE_DISABLE_ERROR_MESSAGE);
        }

        $ids    = $this->request->param('ids', '', 'trim');
        $status = $this->request->param('status', '0', 'intval');
        if (empty($ids) || $status == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $articleBusiness = new \app\common\business\Article($this->websiteCode, $module['code']);
        $res             = $articleBusiness->batchUpdateArticleStatus(explode(',', $ids), $status);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

}
